/*									             Can I Stay a BIT Longer?
										The Effect of BITs on Political Survival
												     Shom Mazumder
												  (Harvard University)
						
											   	    August 2015
							            prep leader data for matching algorithm
*/

cd "INSERT WORKING DIRECTORY HERE"

use "Replication.dta", clear

***************************************************************************
* The following code will drop all "post treatment" observations from the *
* dataset for those leaders who sign a BIT into force.					  *
***************************************************************************

sort leadid year

//generate BIT "treatment" variable for matching procedures
gen BIT_lead_sign=0
replace BIT_lead_sign=1 if NSBITs>0

//generate log real gdp per cap
gen logrealgdppc = log(rgdpo/pop)

//generate log population
gen logpop = log(pop)

//generate measure of BITs inherited from predecessors
gen inherited_BITs = log(1+totalBITs-(NSBITs+SSBITs))

gen prev_force=.

replace prev_force= 1 if BIT_lead_sign[_n-1]==1 & leadid==leadid[_n-1]

drop if prev_force==1

drop prev_force


***************************************************************************
* The following will collapse the data by leader taking the mean value of *
* all pre-treatment controls for eventual BIT signatories and the mean 	  *
* value over the whole dataset for non-signatories.						  *
* 																		  *
* NOTE: Matching procedure uses whether the leader ever signed a PTA      *
* rather than the count to guard against incorrectly matching on          *
* post-treatment covariates given Busch and Tobin (2010)			      *
***************************************************************************


collapse (mean) polity growth logpop logrealgdppc inherited_BITs (max) BIT_lead_sign (first) ccode country leader, by(leadid)


***************************************************************************
* The following will rectangularize the dataset.					      *
***************************************************************************

drop if polity==.
drop if growth==.
drop if logpop==.
drop if logrealgdppc==.
drop if BIT_lead_sign==.
drop if inherited_BITs==.


***************************************************************************
* The following will generate weights for observations.				      *
***************************************************************************

saveold "MatchCollapse.dta", replace

**NOW YOU NEED TO RUN THE R SCRIPT THAT RUNS THE GENETIC MATCHING ALGORITHM**
